<template>
	<view>
		<u-tabs :list="tabList" :is-scroll="false" :current="currTab" @change="changeTab"></u-tabs>

		<view style="padding:  0 20rpx;">
			<view v-for="(item, index) in dataList" :key="item.id" class="order-item bg-white mt20">
				<view class="order-header row-between">
					<view class="muted xs row-center" style="gap: 10rpx;">
						<view>订单编号:{{item.order_sn}}</view>
						<image @tap="toCopy(item.order_sn)" src="../../static/images/copy_icon.png"
							style="width: 30rpx;" mode="widthFix">
						</image>
					</view>
					<view class="row" style="gap:10rpx">
						<view :style="{'color':tagStyle(item)}">
							{{item.status_name}}
						</view>
					</view>
				</view>
				<view class="order-content row">
					<view class="order-goods-img">
						<u-image width="100%" height="100%" :src="item.avatar" />
					</view>
					<view class="order-goods-info ml20">
						<view class="row-between">
							<view class="name row bold">{{item.goods_name}}</view>
						</view>
						<view class="row-between">
							<view class="phone-row">
								<text class="text">下单时间：{{item.create_time}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="goods-info-box">
					<view class="content-box">
						<view class="row-between">
							<view class="left row">
								<template v-if="item.order_status == 3">
									<view class="label">应归还时间：</view>
									<view class="text">{{item.end_time}}</view>
								</template>
								<template v-else>
									<view class="label">归还时间：</view>
									<view class="text">{{item.end_time}}</view>
								</template>
							</view>
							<view class="right">租期共：{{item.goods_info}}</view>
						</view>
					</view>
				</view>
				<view class="mt20 row-end" style="padding: 0 20rpx;gap:20rpx">
					<u-button v-if="item.order_status==3" size="mini" shape="circle" type="primary" plain="true"
						@click="toClick(1,item.id)">去续租</u-button>
					<u-button v-if="item.order_status==3" size="mini" shape="circle" type="error"
						@click="toClick(2,item.id)">去归还</u-button>
					<u-button v-if="item.order_status==6&&item.status==1" size="mini" shape="circle" type="error"
						@click="toClick(4,item.id)">去查看</u-button>
					<u-button v-if="item.order_status==6&&item.status==1" size="mini" shape="circle" type="error"
						@click="toClick(6,item.id)">支付租金</u-button>
					<u-button v-if="item.order_status==6 && item.status==2" size="mini" shape="circle" type="error"
						@click="toClick(2,item.id)">去修改</u-button>
					<u-button v-if="item.order_status==6&&item.status==1&&item.leixin==3" size="mini" shape="circle"
						type="error" @click="toClick(5,item.id)">去寄出</u-button>
				</view>
			</view>
		</view>

		<loading-footer :status="status"></loading-footer>

		<!-- 选择期数弹窗 -->
		<u-popup v-model="popupShow" mode="bottom" height="50%" :border-radius="24" :closeable="true"
			:safe-area-inset-bottom="true"
			:custom-style="{'backgroundColor':'#f8f8f8','padding-top':'30rpx','display':'flex','flex-direction':'column'}">
			<view style="height: 100%; position: relative;">
				<view class="row-center xxl">请选择期数</view>
				<view style="padding: 0 20rpx;" class="mt30">
					<view v-if="periodsList&&periodsList.length>0" class="bg-white row wrap"
						style="border-radius: 20rpx;padding: 30rpx 20rpx;gap: 30rpx;">
						<u-button v-for="item in periodsList" :key="item.periods_id" shape="circle" plain
							:throttle-time="0" :type="currPeriod.periods_id==item.periods_id?'primary':'default'"
							size="medium" :custom-style="{'padding':'0 40rpx'}"
							@click="currPeriod=item">{{item.name}}</u-button>
					</view>
					<u-button type="primary" shape="circle" @click="popupConfirm"
						style="position: absolute;bottom: 0;width:calc(100% - 40rpx)">立即续租</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		copy
	} from '../../../utils/tools';
	import {
		getGiveList,
		postContinueLease
	} from '../../../api/user';
	import {
		loadingFun
	} from '../../../utils/tools';
	import {
		loadingType
	} from '../../../utils/type';
	const tabsList = [{
		id: null,
		name: '全部'
	}, {
		id: 3,
		name: '待归还'
	}, {
		id: -1,
		name: '待支付'
	}, {
		id: 4,
		name: '已结清'
	}, {
		id: 6,
		name: '已审核'
	}]
	export default {
		data() {
			return {
				tabList: tabsList,
				currTab: 1,
				page: 1,
				status: loadingType.LOADING,
				dataList: [],
				popupShow: false,
				periodsList: null,
				currPeriod: null,
				orderid: null,
			}
		},
		onLoad() {
			this.getDataList();
			uni.$on('reload-list', () => {
				this.clear();
				this.getDataList();
			})
		},
		onReachBottom() {
			this.getDataList();
		},
		methods: {
			popupConfirm() {
				this.popupShow = false;
				uni.navigateTo({
					url: `/package_user/pages/order_submit/order_submit?id=${this.orderid}&type=continue&periods_id=${this.currPeriod.periods_id}`
				})
			},
			toCopy(text) {
				copy(text)
			},
			toClick(type, id) {
				let url;
				this.orderid = id;
				switch (type) {
					case 1:
						// url = "/package_user/pages/continue_to_lease/continue_to_lease"
						this.popupShow = true;
						postContinueLease({
							id,
							action: 'info'
						}).then(res => {
							if (res.code != 1) return this.$toast({
								title: res.msg
							});
							this.periodsList = res.data;
							this.currPeriod = res.data[0];
						})
						break;
					case 2:
						url = "/package_user/pages/to_give_back/to_give_back"
						break;
					case 3:
						break;
					case 4:
						url = "/package_user/pages/review_status/review_status"
						break;
					case 5:
						url = '/package_user/pages/to_give_back_express/to_give_back_express'
						break;
					case 6:
						url = '/package_user/pages/order_submit/order_submit';
						break;
					default:
						break;
				}
				if (!url) return;
				uni.navigateTo({
					url: url + '?id=' + id
				})
			},
			changeTab(e) {
				this.currTab = e;
				this.clear();
				this.getDataList();
			},
			clear() {
				this.page = 1;
				this.status = loadingType.LOADING;
				this.dataList = [];
			},
			tagStyle(item) {
				let color = '#FF2C3C';
				//订单状态:3租用中,-1待支付,4已结清，6已审核,空为全部
				switch (item.order_status) {
					case 4:
						color = '#666';
						break;
				}
				return color
			},
			async getDataList() {
				const data = await loadingFun(getGiveList, this.page, this.dataList, this.status, {
					status: this.tabList[this.currTab].id
				})
				if (data) {
					this.page = data.page;
					this.status = data.status;
					this.dataList = data.dataList;
				}
			},
			onunload() {
				uni.$off('reload-list')
			}
		}
	}
</script>

<style lang="scss">
	.order-item {
		border-radius: 14rpx;
		padding-bottom: 30rpx;

		.order-header {
			padding: 20rpx 30rpx;
			border-bottom: var(--border);

			.status {
				color: #F95F2F;
			}
		}

		.order-content {
			padding: 20rpx 30rpx 20rpx 20rpx;
			border-top: 1px solid #E5E5E5;

			.order-goods-img {
				width: 140rpx;
				height: 140rpx;
				flex: none;
				border-radius: 50%;
				overflow: hidden;
			}

			.order-goods-info {
				flex: 1;
				display: flex;
				flex-direction: column;
				text-align: left;

				.name {
					line-height: 36rpx;
					font-weight: 500;
					font-size: 32rpx;
				}

				.address {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #333333;
					line-height: 24rpx;
				}

				.phone-row {
					display: flex;
					align-items: center;
					margin-top: 20rpx;

					.icon1 {
						width: 24rpx;
						height: auto;
					}

					.icon2 {
						width: 36rpx;
						height: 36rpx;
					}

					.text {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #666;
						line-height: 24rpx;
						margin-top: 20rpx;
					}
				}
			}
		}

		.goods-info-box {
			margin-top: 30rpx;

			.content-box {
				width: 654rpx;
				background: #F5F8FA;
				border-radius: 6rpx;
				margin: 0 auto;
				padding: 32rpx 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #666666;
				line-height: 28rpx;

				.text {
					color: #FF2C3C;
					font-weight: 500;
				}

				.right {
					color: #333333;
					font-weight: 500;
				}

				.date {
					margin-top: 24rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
					line-height: 24rpx;
					min-width: 350rpx;
				}
			}
		}

		.address-box {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #333;
			line-height: 40rpx;
			margin-top: 30rpx;
			padding: 0 28rpx;

			.dot {
				width: 16rpx;
				height: 16rpx;
				background: #FBB420;
				border-radius: 50%;
				margin-right: 16rpx;
			}

			.label {
				color: #b0b0b0;
			}

			.address {
				padding-left: 32rpx;
				margin-top: 5rpx;
			}
		}

	}
</style>